use "build/output/main_panel.dta", clear
tsset coin_id date
sort coin_id date


gen d_margin_aprFTX_bps = d.margin_aprFTX*100

eststo clear
eststo: ivreg2 d_margin_aprFTX_bps (d_ln_face_bps = l.funding_apr l.iv_btc) i.mdate  if coin == "USDT" , robust cluster(wdate)
estadd local first_stage_controls "No"
estadd local month_fe "Yes"
estadd local coin_fe "n/a"
estadd local sample "Full"

eststo: ivreg2 d_margin_aprFTX_bps (d_ln_face_bps = l.funding_apr l.iv_btc) funding_apr iv_btc  i.mdate if coin == "USDT",  robust cluster(wdate)
estadd local first_stage_controls "No"
estadd local month_fe "Yes"
estadd local coin_fe "n/a"
estadd local sample "Full"

eststo: ivreg2 d_margin_aprFTX_bps (d_ln_face_bps = l.funding_apr l.iv_btc l.d_ln_face_bps l.ln_face l.ln_vol_btc l.d_ln_vol_btc) funding_apr iv_btc i.mdate if coin == "USDT",  robust cluster(wdate)
estadd local first_stage_controls "Yes"
estadd local month_fe "Yes"
estadd local coin_fe "n/a"
estadd local sample "Full"

eststo: ivreg2 d_margin_aprFTX_bps (d_ln_face_bps = l.funding_apr l.iv_btc l.d_ln_face_bps l.ln_face l.ln_vol_btc l.d_ln_vol_btc) funding_apr iv_btc i.mdate if coin == "USDT" & mdate !=ym(2022,5),  robust cluster(wdate)
estadd local first_stage_controls "Yes"
estadd local month_fe "Yes"
estadd local coin_fe "n/a"
estadd local sample "Excl. 5/22"

eststo: ivreg2 d_margin_aprFTX_bps (d_ln_face_bps = l.funding_apr l.iv_btc) i.mdate i.coin_id, robust cluster(wdate)
estadd local first_stage_controls "No"
estadd local month_fe "Yes"
estadd local coin_fe "Yes"
estadd local sample "Full"

eststo: ivreg2 d_margin_aprFTX_bps (d_ln_face_bps = l.funding_apr l.iv_btc) funding_apr iv_btc  i.mdate i.coin_id,  robust cluster(wdate)
estadd local first_stage_controls "No"
estadd local month_fe "Yes"
estadd local coin_fe "Yes"
estadd local sample "Full"

eststo: ivreg2 d_margin_aprFTX_bps (d_ln_face_bps = l.funding_apr l.iv_btc l.d_ln_face_bps l.ln_face l.ln_vol_btc l.d_ln_vol_btc) funding_apr iv_btc i.mdate i.coin_id,  robust cluster(wdate)
estadd local first_stage_controls "Yes"
estadd local month_fe "Yes"
estadd local coin_fe "Yes"
estadd local sample "Full"

eststo: ivreg2 d_margin_aprFTX_bps (d_ln_face_bps = l.funding_apr l.iv_btc l.d_ln_face_bps l.ln_face l.ln_vol_btc l.d_ln_vol_btc) funding_apr iv_btc i.mdate i.coin_id if mdate !=ym(2022,5),  robust cluster(wdate)
estadd local first_stage_controls "Yes"
estadd local month_fe "Yes"
estadd local coin_fe "Yes"
estadd local sample "Excl. 5/22"

esttab, drop(*.mdate *.coin_id _cons) star(* 0.10 ** 0.05 *** 0.01) r2

esttab using "analysis/output_tables/regression_prop_peg_stability_second_stage.tex", replace booktabs label drop(*.mdate *.coin_id _cons) ///
	mgroups("USDT" "USDT and DAI", pattern(1 0 0 0 1 0 0 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
	prehead(\begin{tabular}{l S S S S S S S S} \toprule) ///
	posthead(\midrule) postfoot(\bottomrule\end{tabular}) ///
	style(tex) substitute(\_ _) star  b(2) t(2)  ///
	nomtitles /// 
	varlabels(d_ln_face_bps "\$\widehat{\Delta \ln(s_{i,t})}\$" iv_btc "\$\text{Bitcoin Implied Volatility}_{t}\$" funding_apr "\$\text{Funding Premium}_{t}\$" L.ln_vol_btc "\$\ln(\text{BTC Vol}_{t-1})\$" L.d_ln_vol_btc "\$\Delta \ln(\text{BTC Vol}_{t-1})\$") /// 
	star(* 0.10 ** 0.05 *** 0.01) ///
	stats(N month_fe coin_fe first_stage_controls sample, fmt(%9.0fc 2) layout("\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}" "\multicolumn{1}{c}{@}") labels(`"\$N\$"' `"Month FE"' `"Coin FE"' `"Controls"' `"Sample"' ))
	
